import {http} from '@/plugins/net.js'

/**
 * 分页查询用户信息
 * @param username
 * @param currPage
 * @param pageSize
 * @returns {Promise<AxiosResponse<any>>}
 */
export const list = async (username,currPage,pageSize) => {
    let url = '/user/list?1=1'
    if (username) {
        url = url + `&username=${username}`
    }
    if (currPage) {
        url += `&currentPage=${currPage}`
    }
    if (pageSize) {
        url += `&pageSize=${pageSize}`
    }
    const res = await http(url,'get',null)
    return res
}

/**
 * 添加用户
 * @param user
 * @returns {Promise<void>}
 */
export  const add = async (user) => {
    let url = `/user/add`
    const res = await http(url,'post',user)
}

/**
 * 修改用户
 * @param user
 * @returns {Promise<void>}
 */
export const update = async (user) => {
    let url = `/user/update`
    const res = await http(url,'put',user)
}

/**
 * 删除用户
 * @param ids
 * @returns {Promise<void>}
 */
export const deleteUser = async (ids) => {
    let url = `/user/delete?1=1`
    if (ids && ids instanceof Array) {
        ids.forEach(id => {
            url += `&ids=${id}`
        })
    }
    const res = await http(url,'delete')
}



/**
 * 查询用户没有被分配的角色
 * @param userId
 * @returns {Promise<AxiosResponse<any>>}
 */
export const listNoHavRoleForUser = async (userId) => {
    let url = `/role/userNoHav?userId=${userId}`
    const res = await http(url,'get',null)
    if (res) {
        return res
    }
}

/**
 * 修改用户状态
 * @param userId
 * @returns {Promise<AxiosResponse<any>>}
 */
export const changeStatus = (userId) => {
    let url = `/user/modifyStatus?userId=${userId}`
    return  http(url,'put',null)
}


/**
 * 给用户分配角色
 * @param userId
 * @param roleIds
 * @returns {Promise<AxiosResponse<any>>}
 */
export const dispatchRole = (userId,roleIds) => {
    let url = `/user/addRole?userId=${userId}`
    roleIds.forEach(roleId => {
        url += `&roleIds=${roleId}`
    })
    return http(url,'post',null)
}

/**
 * 删除用户角色
 * @param userId
 * @param roleId
 */
export const deleteRole = (userId,roleId) => {
    let url = `/user/deleteRole?userId=${userId}&roleId=${roleId}`
    return http(url,'delete')
}